package com.mdd.admin.service.log.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.log.ISmsLogService;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.validate.log.SmsLogParam;
import com.mdd.admin.vo.log.SmsLogDetailVo;
import com.mdd.admin.vo.log.SmsLogListVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.log.SmsLog;
import com.mdd.common.mapper.log.SmsLogMapper;
import com.mdd.common.utils.TimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 短信发送记录实现类
 */
@Service
public class SmsLogServiceImpl implements ISmsLogService {
        
    @Resource
    SmsLogMapper smsLogMapper;

    /**
     * 短信发送记录列表
     *
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return PageResult<SmsLogListVo>
     */
    @Override
    public PageResult<SmsLogListVo> list(PageParam pageParam, Map<String, String> params) {
        Integer page  = pageParam.getPageNo();
        Integer limit = pageParam.getPageSize();

        QueryWrapper<SmsLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        smsLogMapper.setSearch(queryWrapper, params, new String[]{
            "=:sceneId@scene_id:long",
            "like:mobile:str",
            "=:content:str",
            "=:code:str",
            "=:isVerify@is_verify:int",
            "=:checkNum@check_num:int",
            "=:sendStatus@send_status:int",
            "=:sendTime@send_time:int",
            "=:results:str",
        });

        IPage<SmsLog> iPage = smsLogMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<SmsLogListVo> list = new LinkedList<>();
        for(SmsLog item : iPage.getRecords()) {
            SmsLogListVo vo = new SmsLogListVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtil.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtil.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 短信发送记录详情
     *
     * @param id 主键参数
     * @return SmsLog
     */
    @Override
    public SmsLogDetailVo detail(Integer id) {
        SmsLog model = smsLogMapper.selectOne(
                new QueryWrapper<SmsLog>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        SmsLogDetailVo vo = new SmsLogDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 短信发送记录新增
     *
     * @param smsLogParam 参数
     */
    @Override
    public void add(SmsLogParam smsLogParam) {
        SmsLog model = new SmsLog();
        model.setSceneId(smsLogParam.getSceneId());
        model.setMobile(smsLogParam.getMobile());
        model.setContent(smsLogParam.getContent());
        model.setCode(smsLogParam.getCode());
        model.setIsVerify(smsLogParam.getIsVerify());
        model.setCheckNum(smsLogParam.getCheckNum());
        model.setSendStatus(smsLogParam.getSendStatus());
        model.setSendTime(smsLogParam.getSendTime());
        model.setResults(smsLogParam.getResults());
        smsLogMapper.insert(model);
    }

    /**
     * 短信发送记录编辑
     *
     * @param smsLogParam 参数
     */
    @Override
    public void edit(SmsLogParam smsLogParam) {
        SmsLog model = smsLogMapper.selectOne(
                new QueryWrapper<SmsLog>()
                    .eq("id",  smsLogParam.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(smsLogParam.getId());
        model.setSceneId(smsLogParam.getSceneId());
        model.setMobile(smsLogParam.getMobile());
        model.setContent(smsLogParam.getContent());
        model.setCode(smsLogParam.getCode());
        model.setIsVerify(smsLogParam.getIsVerify());
        model.setCheckNum(smsLogParam.getCheckNum());
        model.setSendStatus(smsLogParam.getSendStatus());
        model.setSendTime(smsLogParam.getSendTime());
        model.setResults(smsLogParam.getResults());
        smsLogMapper.updateById(model);
    }

    /**
     * 短信发送记录删除
     *
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        SmsLog model = smsLogMapper.selectOne(
                new QueryWrapper<SmsLog>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        smsLogMapper.delete(new QueryWrapper<SmsLog>().eq("id", id));
    }

}
